package com.demo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.common.R;
import com.demo.domain.Employee;
import com.demo.service.EmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("/employee")
@Api(tags = "员工登录详情")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 员工登录验证
     * @param request
     * @param employee
     * @return
     */
    @ApiOperation(value = "用户登录")
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        return employeeService.login(request, employee);
    }

    /**
     * 员工退出功能
     * @return 步骤如下
     * 1.将session中存储的id删除
     * 2.返回退出信息
     */
    @ApiOperation(value = "用户退出")
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {
        return employeeService.logout(request);
    }

    /**
     * 员工信息保存功能
     * @param employee
     * @return
     */
    @ApiOperation(value = "新增用户信息")
    @PostMapping
    public R<String> save(HttpServletRequest request,@RequestBody Employee employee){
        return employeeService.save(request,employee);
    }

    /**
     * 分页查询所有员工数据
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @ApiOperation(value = "分页查询用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页码",readOnly = true),
            @ApiImplicitParam(name = "pageSize",value = "每页登录数",readOnly = true),
            @ApiImplicitParam(name = "name",value = "员工姓名",readOnly = false)
    })
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        return employeeService.page(page,pageSize,name);
    }

    /**
     * 修改员工信息
     * @param request
     * @param employee
     * @return
     */
    @PutMapping
    @ApiOperation(value = "修改用户信息")
    public R<String> update(HttpServletRequest request,@RequestBody Employee employee){
        return employeeService.update(request,employee);
    }

    /**
     * 根据id回显数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "回显用户信息")
    public R<Employee> getById(@PathVariable Long id){
        log.info("修改状态的id为 {}" ,id.toString());

        return employeeService.getById(id);
    }

    /**
     * 删除员工
     * @param id
     * @return
     */
    @ApiOperation(value = "删除员工")
    @DeleteMapping("/{id}")
    public R<String> delete(@PathVariable Long id){
        log.info("删除的用户id为 {}" ,id.toString());
        return employeeService.delete(id);
    }

}
